package com.mayisoftware.mayioa.business.statistic.impl.serviceImpl;

import com.mayisoftware.mayioa.business.statistic.api.domain.StatisDate;
import com.mayisoftware.mayioa.business.statistic.api.domain.StatisDept;
import com.mayisoftware.mayioa.business.statistic.api.domain.StatisType;
import com.mayisoftware.mayioa.business.statistic.api.service.IStatisticService;
import com.mayisoftware.mayioa.business.statistic.impl.dao.StatisticMapper;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.api.domain.SysDept;
import com.mayisoftware.mayioa.platform.api.domain.SysDictData;
import com.mayisoftware.mayioa.platform.api.service.ISysDeptService;
import com.mayisoftware.mayioa.platform.api.service.ISysDictDataService;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 骨干统计服务实现
 */
@Service
public class StatisticServiceImpl implements IStatisticService {


    public static final String DEPT_MOUDLE ="*,*,BG,BU,CU";

    @Autowired
    ISysDeptService sysDeptService;

    /**
     * 统计服务dao
     */
    @Autowired
    private StatisticMapper statisticMapper;

    /**
     * 数据字典服务
     */
    @Autowired
    ISysDictDataService sysDictDataService;

    @Override
    public List<StatisType> queryStatisByType() {
        return statisticMapper.selectStatisByType();
    }

    @Override
    public List<StatisDept> queryStatisByDept() {
        return statisticMapper.selectStatisByDept();
    }

    @Override
    public List<StatisDate> queryStatisByDate() {
        return statisticMapper.selectStatisByDate(ShiroUtils.getUserId());
    }

    @Override
    public List<Map> selectStatisByRegion() {
        return statisticMapper.selectStatisByRegion(ShiroUtils.getUserId());
    }


    /**
     * 从数据字典获取城市经纬度
     * @return
     */
    public Map getGeoCoordMap(){
        List<SysDictData> sysDictDataList =  sysDictDataService.selectDictDataByType("sys_user_region");
        Map geoCoordMap = new HashMap();
        for(SysDictData sysDictData: sysDictDataList){
            String remark = sysDictData.getRemark();
            if(!StringUtils.isEmpty(remark)){
                geoCoordMap.put(sysDictData.getDictValue(), remark.split(","));
            }
        }
        return geoCoordMap;
    }


    public List<StatisDept> selectCountStatisByDept(){
        return statisticMapper.selectCountStatisByDept(ShiroUtils.getUserId());
    }

    /**
     * 按部门类型进行统计
     * @param type
     * @return
     */
    public List<StatisDept> countStatisByDeptType(String type){
        int typeIndex = this.getTypeIndex(type);
        if(typeIndex==-1){
            return null;
        }
        List<StatisDept> list = this.selectCountStatisByDept();
        Map<String,Integer> map = new HashMap<String,Integer>();
        //按照部门类型统计数量
        for(StatisDept sd :list){
            String[] ancestor = sd.getAncestors().split(",");
            if(ancestor.length<typeIndex){
                continue;
            }
            String deptId = "";
            if(ancestor.length==typeIndex){
                deptId = sd.getDeptId();
            }else{
                deptId = ancestor[typeIndex];
            }
            Integer num = sd.getNum();
            if(map.get(deptId)!=null){
                num+=map.get(deptId);
            }
            map.put(deptId,num);
        }

        List<StatisDept> newList = new ArrayList<StatisDept>();
        for(Map.Entry<String,Integer> entry :map.entrySet()){
            StatisDept sd = new StatisDept();
            sd.setDeptId(entry.getKey());
            sd.setNum(entry.getValue());
            SysDept dept = sysDeptService.selectDeptById(Long.valueOf( sd.getDeptId()));
            sd.setDeptName(dept.getDeptName());
            newList.add(sd);
        }

        return newList;
    }


    /**
     * 按部门和骨干类型统计
     */
    public List<StatisDept> selectCountStatisByDeptAndUserType(String type){
        int typeIndex = this.getTypeIndex(type);
        if(typeIndex==-1){
            return null;
        }
        List<StatisDept> list = statisticMapper.selectCountStatisByDeptAndUserType(ShiroUtils.getUserId());
        Map<String,Map<String,Integer>> map = new HashMap<String,Map<String,Integer>>();
        //按照部门类型统计数量
        for(StatisDept sd :list){
            String[] ancestor = sd.getAncestors().split(",");
            if(ancestor.length<typeIndex){
                continue;
            }
            if(StringUtils.isEmpty(sd.getProjectRole())){
                continue;
            }
            String deptId = "";
            if(ancestor.length==typeIndex){
                deptId = sd.getDeptId();
            }else{
                deptId = ancestor[typeIndex];
            }
            Map<String,Integer> projectRoleCountMap =map.get(deptId);
            if(projectRoleCountMap!=null){
                Integer num = projectRoleCountMap.get(sd.getProjectRole());
                if(num==null){
                    num= sd.getNum();
                }else{
                    num+=sd.getNum();
                }
                projectRoleCountMap.put(sd.getProjectRole(),num);
            }else{
                projectRoleCountMap =new HashMap<String, Integer>();
                projectRoleCountMap.put(sd.getProjectRole(),sd.getNum());
            }
            map.put(deptId,projectRoleCountMap);
        }

        List<StatisDept> newList = new ArrayList<StatisDept>();
        for(Map.Entry<String,Map<String,Integer>> entry :map.entrySet()){
            StatisDept sd = new StatisDept();
            sd.setDeptId(entry.getKey());
            sd.setProjectRoleCount(entry.getValue());
            SysDept dept = sysDeptService.selectDeptById(Long.valueOf( sd.getDeptId()));
            sd.setDeptName(dept.getDeptName());
            newList.add(sd);
        }
        return newList;
    }

    /**
     * 按部门和时间类型统计
     */
    public List<StatisDept> selectCountStatisByDeptAndDateInfo(String type){
        int typeIndex = this.getTypeIndex(type);
        if(typeIndex==-1){
            return null;
        }
        List<StatisDept> list = statisticMapper.selectCountStatisByDeptAndDateInfo(ShiroUtils.getUserId());
        Map<String,Map<String,Integer>> map = new HashMap<String,Map<String,Integer>>();
        //按照部门类型统计数量
        for(StatisDept sd :list){
            String[] ancestor = sd.getAncestors().split(",");
            if(ancestor.length<typeIndex){
                continue;
            }
            String deptId = "";
            if(ancestor.length==typeIndex){
                deptId = sd.getDeptId();
            }else{
                deptId = ancestor[typeIndex];
            }
            Map<String,Integer> projectRoleCountMap =map.get(deptId);
            if(projectRoleCountMap!=null){
                Integer num = projectRoleCountMap.get(sd.getDateInfo());
                if(num==null){
                    num= sd.getNum();
                }else{
                    num+=sd.getNum();
                }
                projectRoleCountMap.put(sd.getDateInfo(),num);
            }else{
                projectRoleCountMap =new HashMap<String, Integer>();
                projectRoleCountMap.put(sd.getDateInfo(),sd.getNum());
            }
            map.put(deptId,projectRoleCountMap);
        }

        List<StatisDept> newList = new ArrayList<StatisDept>();
        for(Map.Entry<String,Map<String,Integer>> entry :map.entrySet()){
            StatisDept sd = new StatisDept();
            sd.setDeptId(entry.getKey());
            sd.setProjectRoleCount(entry.getValue());
            SysDept dept = sysDeptService.selectDeptById(Long.valueOf( sd.getDeptId()));
            sd.setDeptName(dept.getDeptName());
            newList.add(sd);
        }
        return newList;
    }

    @Override
    public List<StatisDept> selectCountPMStatisByProjectRole(Long userId) {
        return  statisticMapper.selectCountPMStatisByProjectRole(userId);
    }

    @Override
    public List<StatisDept> selectCountPMStatisByDateInfo(Long userId) {
        return statisticMapper.selectCountPMStatisByDateInfo(userId);
    }


    /**
     * 判断部门类型的位置
     */
    public int getTypeIndex(String type){
        if(StringUtils.isEmpty(type)){
            return -1;
        }
        String [] mtypes =DEPT_MOUDLE.split(",");
        for(int i=0;i<mtypes.length;i++){
            if(type.equals(mtypes[i])){
                return i ;
            }
        }
        return -1;
    }
}